package com.dxq.test.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.dxq.test.common.AppManager;
import com.dxq.test.R;
import com.dxq.test.db.Pasture;
import com.dxq.test.db.TablesDao;
import com.dxq.test.utils.DialogClickListener;
import com.dxq.test.utils.MyUtil;

import java.util.List;

/**
 * Author: zsh
 * Date: 2023/12/3
 * Describe: 牧场添加、修改、删除页面
 */
public class PastureAddActivity extends AppCompatActivity implements View.OnClickListener {

    public TablesDao tablesDao;
    private String[] types = {"请选择", "牛", "羊", "禽类"};

    private ImageView ivBack;
    private TextView tvTitle, tvSet;
    private Button btnSubmit;
    private EditText etName, etInfo;
    private Spinner spType;
    private int type = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加Activity到堆栈
        AppManager.getAppManager().addActivity(this);
        setContentView(R.layout.activity_pasture_add);
        tablesDao = new TablesDao(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在appManager结束当前activity
        AppManager.getAppManager().finishActivity(this);
    }

    private void initView() {
        ivBack = findViewById(R.id.ivBack);
        tvTitle = findViewById(R.id.tvTitle);
        tvSet = findViewById(R.id.tvSet);
        etName = findViewById(R.id.etName);
        spType = findViewById(R.id.spType);
        etInfo = findViewById(R.id.etInfo);
        btnSubmit = findViewById(R.id.btnSubmit);

        tvTitle.setText("新建牧场");
//        if (null == event) {
//            tvTitle.setText("添加事件");
//            btnSubmit.setText("新增");
//            tvSet.setVisibility(View.GONE);
//        } else {
//            tvTitle.setText("修改事件");
//            btnSubmit.setText("修改");
//            tvSet.setVisibility(View.VISIBLE);
//            tvSet.setText("删除");
//            etName.setText(event.getName());
//            etInfo.setText(event.getInfo());
//        }

        ivBack.setOnClickListener(this);
        tvSet.setOnClickListener(this);
        btnSubmit.setOnClickListener(this);

        // 声明一个ArrayAdapter用于存放简单数据
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                PastureAddActivity.this, android.R.layout.simple_spinner_dropdown_item, types);
        // 把定义好的Adapter设定到spinner中
        spType.setAdapter(adapter);
        // 为第一个Spinner设定选中事件
        spType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                //在选中之后触发
                type = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // 这个一直没有触发，我也不知道什么时候被触发。
                //在官方的文档上说明，为back的时候触发，但是无效，可能需要特定的场景
            }
        });

        //默认值
        spType.setSelection(type);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivBack:
                this.finish();
                break;

            case R.id.tvSet://删除
                MyUtil.normalDialog(PastureAddActivity.this, "操作提示", "是否删除当前事件？",
                        "取消", "确定", new DialogClickListener() {

                            @Override
                            public void onNegativeClick() {

                            }

                            @Override
                            public void onPositiveClick() {
                                //deleteEvent();
                            }
                        });
                break;

            case R.id.btnSubmit://提交
                submit();
                break;
        }
    }

    //删除event
//    private void deleteEvent() {
//        int result = tablesDao.deleteEvent(event);
//        if (result != 1) {
//            ToastUtils.showShort("删除失败, event");
//            return;
//        }
//        //接着删除关联表
//        List<LabelEvent> labelEvents = tablesDao.queryLabelEventByEvent(event);
//        int res = tablesDao.deleteLabelEvent(labelEvents);
//        ToastUtils.showShort("删除成功");
//        this.finish();
//    }
//
    private void submit() {
        String name = etName.getText().toString();
        String info = etInfo.getText().toString();
        if (name.equals("")) {
            ToastUtils.showShort("输入牧场名称");
            return;
        }
        if (type <= 0) {
            ToastUtils.showShort("选择养殖品种");
            return;
        }
        //判断该名称是否已注册
        List<Pasture> pastures = tablesDao.queryPastureByName(name);
        if (!ObjectUtils.isEmpty(pastures)) {
            ToastUtils.showShort("'" + name + "'牧场已经注册");
            return;
        }
        //if (null == event) {
        //新增
        String nowTime = TimeUtils.getNowString();
        Pasture pasture = new Pasture(name, String.valueOf(type - 1), info, nowTime, nowTime, null);
        //1、创建牧场
        int res = tablesDao.insertPasture(pasture);
        if (res != 1) {
            ToastUtils.showShort("'" + name + "'牧场注册失败");
            return;
        }
        ToastUtils.showShort("'" + name + "'牧场注册成功");
        this.finish();
//        } else {
//            //修改
//            Event e = tablesDao.queryEventById(event.getEvent_id());
//            e.setName(name);
//            e.setInfo(info);
//            int result = tablesDao.updateEvent(e);
//            if (result == 1) {
//                ToastUtils.showShort("更新成功");
//                this.finish();
//                return;
//            }
//            ToastUtils.showShort("更新失败");
//        }
    }

    @Override
    public void onBackPressed() {
        this.finish();
    }
}
